package com.washmadam.service.controller.common;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.excel.poi.function.ExportFunction;
import com.github.pagehelper.PageInfo;
import com.washmadam.service.common.base.BaseController;
import com.washmadam.service.common.base.RequestDTO;
import com.washmadam.service.common.constants.Constant;
import com.washmadam.service.common.enums.OrderStatusEnum;
import com.washmadam.service.common.utils.DateUtils;
import com.washmadam.service.common.utils.ExcelUtils;
import com.washmadam.service.db.dto.CustomerOrder;
import com.washmadam.service.db.request.query.CustomerOrderQueryParam;
import com.washmadam.service.db.response.CustomerOrderResponse;
import com.washmadam.service.db.response.excel.CustomerOrderExcelResponse;
import com.washmadam.service.domain.order.CustomerOrderDomain;

import common.service.base.annotation.NoCheckSession;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(Constant.URL_PREFIX + "/cms/export")
@Slf4j
public class ExportController extends BaseController {

	@Autowired
	CustomerOrderDomain customerOrderDomain;
	/**
	 * 
	 * @param 会员订单录入
	 * @param request
	 * @return
	 */
	@RequestMapping("/order/exportResponse")
	@NoCheckSession
    public void exportFranchiseStoreResponse(@RequestBody RequestDTO req,HttpServletResponse httpServletResponse) {
		CustomerOrderQueryParam requestParam = JSONObject.parseObject(JSON.toJSONString(req.getData()),
				CustomerOrderQueryParam.class);
		if(requestParam == null) {
			requestParam = new CustomerOrderQueryParam();
		}
		requestParam.setOrderStatus(OrderStatusEnum.COMING_SOON.getCode().toString());
        ExcelUtils.ExportBuilder(httpServletResponse, "预约订单_"+DateUtils.getDate(), CustomerOrderExcelResponse.class).exportMultiSheetResponse(requestParam,
                new ExportFunction<CustomerOrderQueryParam, CustomerOrder>() {
                    @Override
                    public List<CustomerOrder> pageQuery(CustomerOrderQueryParam queryQaram, int pageNum, int pageSize) {
                        //1.将pageNum和pageSize传入使用本组件的开发者自己项目的分页逻辑中
                    	queryQaram.setPageNum(pageNum);
                    	queryQaram.setPageSize(pageSize);
                        //2.调用使用本组件的开发者自己项目的分页查询方法
                    	PageInfo<CustomerOrder> result = customerOrderDomain.getCustomerOrderList(queryQaram);
                        return result.getList();
                    }
                    /**
                     * 将查询出来的每条数据进行转换
                     *
                     * @param cashFlowResponse
                     */
                    @Override
                    public CustomerOrderExcelResponse convert(CustomerOrder customerOrderResponse) {
                    	CustomerOrderExcelResponse response = converCustomerOrder(customerOrderResponse);
                        return response;
                    }
                });
    }
	
	private CustomerOrderExcelResponse converCustomerOrder(
			CustomerOrder customerOrder) {
		if(customerOrder ==null) {
			return new CustomerOrderExcelResponse();
		}
		CustomerOrderResponse customerOrderResponse = (CustomerOrderResponse) customerOrder;
		CustomerOrderExcelResponse response = new CustomerOrderExcelResponse();
		BeanUtils.copyProperties(customerOrder, response);
		response.setReserveDate(DateUtils.formatDate(customerOrderResponse.getReserveDate()));
		return response;
	}
}
